﻿using System;
using System.IO;
using Infrastructure;
using PresentationNamespace;

namespace WebClient
{
    public partial class AreaConverterWebForm : System.Web.UI.Page, IView
    {
        private Presenter presenter;
        
        public string KnownValue
        {
            get { return knownValue.Text; }
        }

        public string KnownMeasure
        {
            get { return knownMeasureType.Text; }
        }

        public string WantedMeasure
        {
            get { return wantedMeasureType.Text; }
        }

        public string WantedValue
        {
            set { wantedValue.Text = value; }
        }

        public string ErrorMessage
        {
            get { return errorMessage.Text; }
            set { errorMessage.Text = value; }
        }

        public object[] MeasureTypes
        {
            set
            {
                var measureTypes = new String[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    knownMeasureType.Items.Add(value[i].ToString());
                    wantedMeasureType.Items.Add(value[i].ToString());
                }
            }
        }

        public string[] LogText
        {
            set
            {
                logText.Items.Clear();
                for (int i = 0; i < value.Length; i++)
                {
                    logText.Items.Add(value[i]);
                }
            }
        }

        public bool IsTxtLoggerSelected
        {
            get
            {
                return loggerTypes.Items[0].Selected;
            }
            set
            {
                loggerTypes.Items[0].Selected = value;
            }
        }

        public bool IsHtmlLoggerSelected
        {
            get
            {
                return loggerTypes.Items[1].Selected;
            }
            set
            {
                loggerTypes.Items[1].Selected = value;
            }
        }

        public string LogFileName
        {
            get { return logFileName.Text; }
        }

        public event Action OnConvertClick;
        public event Action OnSaveLogAsTxtClick;
        public event Action OnShowLogAsTxtClick;
        public event Action OnSaveLogAsHtmlClick;
        public event Action OnShowLogAsHtmlClick;
        public event Action LoggerTypeChanged;

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Presenter"] == null)
            {
                presenter = new Presenter(this, new HtmlLogger());
                Session.Add("Presenter", presenter);
                IsHtmlLoggerSelected = true;
                IsTxtLoggerSelected = false;
            }
            else
            {
                presenter = (Presenter)Session["Presenter"];
                presenter.Setup(this);
            }
            Directory.SetCurrentDirectory(Server.MapPath("~"));
        }


        protected void ConvertButton_Click(object sender, EventArgs e)
        {
            OnConvertClick();
        }

        protected void txtLoggerSelected_CheckedChanged(object sender, EventArgs e)
        {
            LoggerTypeChanged();
        }

        protected void saveAsTxtButton_Click(object sender, EventArgs e)
        {
            OnSaveLogAsTxtClick();
        }

        protected void saveAsHtmlButton_Click(object sender, EventArgs e)
        {
            OnSaveLogAsHtmlClick();
        }

        protected void showAsTxtButton_Click(object sender, EventArgs e)
        {
            OnShowLogAsTxtClick();
        }

        protected void showAsHtmlButton_Click(object sender, EventArgs e)
        {
            OnShowLogAsHtmlClick();
        }

        public ILogger CreateTxtLogger()
        {
            return new TxtLogger();
        }

        public ILogger CreateHtmlLogger()
        {
            return new HtmlLogger();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            OnConvertClick();
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoggerTypeChanged();
        }
    }
}